package com.osig.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;

import com.itextpdf.text.Document;
import com.itextpdf.text.Element;
import com.itextpdf.text.Font;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
import com.itextpdf.tool.xml.XMLWorkerHelper;

public class PDFReport {

	Document document = new Document();// 建立一个Document对象     
    
    private static Font headfont ;// 设置字体大小 
    private static Font keyfont;// 设置字体大小 
    private static Font textfont;// 设置字体大小 
    
    private PdfWriter pdfWriter;
    
    private int maxWidth = 520;
       
   
       
    static{ 
        BaseFont bfChinese; 
        try { 
            //bfChinese = BaseFont.createFont("STSong-Light","UniGB-UCS2-H",BaseFont.NOT_EMBEDDED); 
            bfChinese = BaseFont.createFont("C:\\Windows\\Fonts\\simkai.ttf", BaseFont.IDENTITY_H, false); 
            headfont = new Font(bfChinese, 10, Font.BOLD);// 设置字体大小 
            keyfont = new Font(bfChinese, 8, Font.BOLD);// 设置字体大小 
            textfont = new Font(bfChinese, 8, Font.NORMAL);// 设置字体大小 
        } catch (Exception e) {          
            e.printStackTrace(); 
        }  
    } 
       
       
    public PDFReport(File file) throws FileNotFoundException {
    	this(new FileOutputStream(file), false);
    } 
    public PDFReport(OutputStream outputStream,boolean isTransverse) { 
    	if(isTransverse) {
    		document.setPageSize(PageSize.A4.rotate());// 设置页面大小,A4横向
    	} else {
    		document.setPageSize(PageSize.A4);// 设置页面大小 
    	}
        try { 
        	pdfWriter = PdfWriter.getInstance(document,outputStream); 
           document.open();  
       } catch (Exception e) { 
           e.printStackTrace(); 
       }  
          
          
   }
       
       
     public PdfPCell createCell(String value,Font font,int align){ 
         PdfPCell cell = new PdfPCell(); 
         cell.setVerticalAlignment(Element.ALIGN_MIDDLE);         
         cell.setHorizontalAlignment(align);     
         cell.setPhrase(new Phrase(value,font)); 
        return cell; 
    } 
       
     public PdfPCell createCell(String value,Font font){ 
         PdfPCell cell = new PdfPCell(); 
         cell.setVerticalAlignment(Element.ALIGN_MIDDLE); 
         cell.setHorizontalAlignment(Element.ALIGN_CENTER);  
         cell.setPhrase(new Phrase(value,font)); 
        return cell; 
    } 
   
     public PdfPCell createCell(String value,Font font,int align,int colspan){ 
         PdfPCell cell = new PdfPCell(); 
         cell.setVerticalAlignment(Element.ALIGN_MIDDLE); 
         cell.setHorizontalAlignment(align);     
         cell.setColspan(colspan); 
         cell.setPhrase(new Phrase(value,font)); 
        return cell; 
    } 
    public PdfPCell createCell(String value,Font font,int align,int colspan,boolean boderFlag){ 
         PdfPCell cell = new PdfPCell(); 
         cell.setVerticalAlignment(Element.ALIGN_MIDDLE); 
         cell.setHorizontalAlignment(align);     
         cell.setColspan(colspan); 
         cell.setPhrase(new Phrase(value,font)); 
         cell.setPadding(3.0f); 
         if(!boderFlag){ 
             cell.setBorder(0); 
             cell.setPaddingTop(15.0f); 
             cell.setPaddingBottom(8.0f); 
         } 
        return cell; 
    } 
     public PdfPTable createTable(int colNumber){ 
        PdfPTable table = new PdfPTable(colNumber); 
        try{ 
            table.setTotalWidth(maxWidth); 
            table.setLockedWidth(true); 
            table.setHorizontalAlignment(Element.ALIGN_CENTER);      
            table.getDefaultCell().setBorder(1); 
        }catch(Exception e){ 
            e.printStackTrace(); 
        } 
        return table; 
    } 
     public PdfPTable createTable(float[] widths){ 
            PdfPTable table = new PdfPTable(widths); 
            try{ 
                table.setTotalWidth(maxWidth); 
                table.setLockedWidth(true); 
                table.setHorizontalAlignment(Element.ALIGN_CENTER);      
                table.getDefaultCell().setBorder(1); 
            }catch(Exception e){ 
                e.printStackTrace(); 
            } 
            return table; 
        } 
       
     public PdfPTable createBlankTable(){ 
         PdfPTable table = new PdfPTable(1); 
         table.getDefaultCell().setBorder(0); 
         table.addCell(createCell("", keyfont));          
         table.setSpacingAfter(20.0f); 
         table.setSpacingBefore(20.0f); 
         return table; 
     } 
        
     public void generatePDF(List<String[]> data,String title) throws Exception{ 
        PdfPTable table = createTable(data.get(0).length); 
        table.addCell(createCell(title, keyfont,Element.ALIGN_LEFT,4,false)); 
               
        for(int i=0;i<data.size();i++){ 
        	for(int j=0;j<data.get(i).length;j++) {
        		if(j == 0) {
        			//设置行头
        			table.addCell(createCell(data.get(i)[j], keyfont, Element.ALIGN_CENTER)); 
        		} else {
        			table.addCell(createCell(data.get(i)[j], textfont));
        		}
        	}
        } 
        document.add(table); 
           
        document.close(); 
     } 
     
     public void generatePDF(InputStream inputStream) throws Exception{ 
    	 XMLWorkerHelper.getInstance().parseXHtml(pdfWriter, document,
    			 inputStream, Charset.forName("UTF-8"));
         // step 5
         document.close();
      }
        
     public static void main(String[] args) throws Exception { 
         File file = new File("D:\\text.pdf"); 
         List<String[]> list = new ArrayList<String[]>();
         
         String[] head = {"姓名","年龄","性别","联系方式"};
         String[] xiaoming = {"小明","18","男","1234568799"};
         String[] xiaohong = {"小红","14","女","1234568799"};
         
         list.add(head);
         list.add(xiaoming);
         list.add(xiaohong);
        new PDFReport(file).generatePDF(list,"学生信息列表：");      
    	 
/*    	 File outFile = new File("D:/test.pdf");
    	 
    	 File inFile = new File("C:/Users/wangmu/Documents/HBuilderProjects/test/bank.html");
    	 
    	 URL url = new URL("http://www.baidu.com");
    	 
    	 
    	 new PDFReport(outFile).generatePDF(new FileInputStream(inFile));*/
    	 
    } 
}
